package cn.xz.hfsk.net;

import android.util.Log;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;

/**
 * Created by yangkun on 17/2/21.
 */
public class SendUdp {

    public static void send(String mge, String ip, int port) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                Log.e("sendUdp", "message:" + mge + ",ip:" + ip + ",port:" + port);
                String message = (mge == null ? "plus" : mge);
                DatagramSocket s = null;
                try {
                    s = new DatagramSocket();
                } catch (SocketException e) {
                    e.printStackTrace();
                }
                InetAddress local = null;
                try {
                    local = InetAddress.getByName(ip);
                } catch (UnknownHostException e) {
                    e.printStackTrace();
                }
                int msg_length = message.length();
                byte[] messageByte = message.getBytes();
                DatagramPacket p = new DatagramPacket(messageByte, msg_length, local,
                        port);
                try {
                    s.send(p);
                    s.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }

    public static void send(byte[] message, String ip, int port) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                Log.e("sendUdp", "message:" + message + "ip:" + ip + "port:" + port);
                DatagramSocket s = null;
                try {
                    s = new DatagramSocket();
                } catch (SocketException e) {
                    e.printStackTrace();
                }
                InetAddress local = null;
                try {
                    local = InetAddress.getByName(ip);
                } catch (UnknownHostException e) {
                    e.printStackTrace();
                }
                int msg_length = message.length;
                DatagramPacket p = new DatagramPacket(message, msg_length, local,
                        port);
                try {
                    s.send(p);
                    s.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }

    public void StartListen() {
        // UDP服务器监听的端口
        Integer port = 8903;
        // 接收的字节大小，客户端发送的数据不能超过这个大小
        byte[] message = new byte[100];
        try {
            // 建立Socket连接
            DatagramSocket datagramSocket = new DatagramSocket(port);
            datagramSocket.setBroadcast(true);
            DatagramPacket datagramPacket = new DatagramPacket(message,
                    message.length);
        } catch (SocketException e) {
            e.printStackTrace();
        }

    }
/*    @Override
    public void run() {
        StartListen();
    }*/
}
